package com.crazymaker.springcloud.demo.security;

import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.authentication.logout.LogoutFilter;

public class DemoAuthConfigurer<T extends DemoAuthConfigurer<T, B>, B extends HttpSecurityBuilder<B>> extends AbstractHttpConfigurer<T, B> {

    private DemoAuthFilter authFilter = new DemoAuthFilter();

    @Override
    public void configure(B builder) throws Exception {
        // 设置共享的认证管理器
        authFilter.setAuthenticationManager(builder.getSharedObject(AuthenticationManager.class));
        // 设置认证过滤器，后置处理器包装之后的过滤器
        DemoAuthFilter demoAuthFilter = this.postProcess(authFilter);
        // 在logoutFilter前面添加一个过滤器
        builder.addFilterBefore(demoAuthFilter, LogoutFilter.class);
    }
}
